package io.github.harmonly.skyblock.manager;

import io.github.harmonly.skyblock.SkyBlock;
import io.github.harmonly.skyblock.util.JavaUtil;
import io.github.harmonly.skyblock.util.StableObject;
import org.bukkit.configuration.file.YamlConfiguration;
import org.bukkit.plugin.Plugin;

import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import java.util.logging.Logger;

/**
 * Utility class to manage resources such as configuration jsons and other plugin jsons.
 */
public class ResourcesManager {

    private static Plugin plugin;
    private static final Map<String, StableObject> jsons = new HashMap<>();
    private static final Map<String, YamlConfiguration> configs = new HashMap<>();

    // Private constructor to prevent instantiation
    private ResourcesManager() {
        throw new IllegalStateException("Utility class");
    }

    /**
     * Sets up the resources manager with the specified plugin and loads the default resources.
     *
     * @param plugin The plugin instance.
     */
    public static void setup(Plugin plugin) {
        ResourcesManager.plugin = plugin;
        loadConfig("config");

        loadJson("pets.json");

        loadJson("lang/en_us/time.json");
        loadJson("lang/en_us/message.json");

        loadJson("lang/zh_cn/time.json");
        loadJson("lang/zh_cn/message.json");
    }

    /**
     * Loads a file from the plugin's data folder and saves it to the internal map.
     *
     * @param filePath The path of the file to load.
     */
    public static void loadJson(String filePath) {
        plugin.saveResource(filePath, true);
        jsons.put(filePath, new StableObject(JavaUtil.readJson(filePath)));
    }

    /**
     * Saves a file from the internal map to the disk.
     *
     * @param filePath The path of the file to saveJson.
     */
    public static void saveJson(String filePath) {
        JavaUtil.writeJson(jsons.get(filePath).getRaw(), filePath);
    }

    public static StableObject getJson(String filename, boolean lang) {
        return jsons.get((lang ? "lang/" + SkyBlock.lang() + "/" : "") + filename + ".json");
    }

    public static StableObject getJson(String filename) {
        return getJson(filename, true);
    }

    /**
     * Reloads a file from the disk and updates the internal map.
     *
     * @param filePath The path of the file to reload.
     */
    public static void reloadJson(String filePath) {
        jsons.put(filePath, new StableObject(JavaUtil.readJson(filePath)));
    }

    /**
     * Loads a configuration file from the plugin's data folder.
     *
     * @param configName The name of the configuration file (without .yml extension).
     */
    public static void loadConfig(String configName) {
        File configFile = new File(plugin.getDataFolder(), configName + ".yml");
        if (!configFile.exists()) {
            plugin.saveResource(configName + ".yml", true);
        }
        configs.put(configName, YamlConfiguration.loadConfiguration(configFile));
    }

    /**
     * Saves a configuration file from the internal map to the disk.
     *
     * @param configName The name of the configuration file (without .yml extension).
     */
    public static void saveConfig(String configName) {
        File configFile = new File(plugin.getDataFolder(), configName + ".yml");
        try {
            YamlConfiguration config = configs.get(configName);
            if (config != null) {
                config.save(configFile);
            } else {
                getLogger().warning("Configuration not found: " + configName);
            }
        } catch (IOException e) {
            getLogger().warning("Unable to saveJson configuration file: " + configName + ".yml");
        }
    }

    /**
     * Returns a configuration file from the internal map.
     *
     * @param configName The name of the configuration file (without .yml extension).
     * @return The configuration, or null if not found.
     */
    public static YamlConfiguration getConfig(String configName) {
        return configs.get(configName);
    }

    /**
     * Reloads a configuration file from the disk and updates the internal map.
     *
     * @param configName The name of the configuration file (without .yml extension).
     */
    public static void reloadConfig(String configName) {
        File configFile = new File(plugin.getDataFolder(), configName + ".yml");
        configs.put(configName, YamlConfiguration.loadConfiguration(configFile));
    }

    /**
     * Returns the logger instance from the plugin.
     *
     * @return The logger instance.
     */
    private static Logger getLogger() {
        return plugin.getLogger();
    }
}
